package com.eff.android.fragment.main;

import java.util.List;

import com.eff.android.R;
import com.eff.android.api.model.Model;
import com.eff.android.api.model.Setting;
import com.eff.android.fragment.EBaseFragment;
import com.eff.android.fragment.IFragmentCallback;
import com.eff.android.fragment.main.start.EMainFragmentPronounciation;
import com.eff.android.fragment.options.EMainFragmentOptions;
import com.eff.android.lib.helper.EDatabaseHelper;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import android.view.View.OnClickListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class EMainFragment extends EBaseFragment implements IFragmentCallback{
	
	public static String TAG = EMainFragment.class.getSimpleName();
	private ViewHolder holder;
	
	private Button btnStart;
	private Button btnOptions;
	private Button btnIntro;
	private Button btnExit;
	
	private FragmentManager eFragmentManager;
    private FragmentTransaction eFragmentTransaction;
	
	public static EMainFragment newInstance (Context context, Bundle b){	
		EMainFragment f = new EMainFragment();
		if (b != null)
			f.setArguments(b);
		return f;		
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		holder = ViewHolder.newInstance(this.getArguments());
		((EMainFragmentPronounciation) holder.start).setCallback(this);
		((EMainFragmentOptions) holder.options).setCallback(this);
		
		eFragmentManager = getFragmentManager();
		
		Log.d("app name", getActivity().getApplicationContext().getPackageName());
    }
	
	 @Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container,
		    Bundle savedInstanceState) {
			 super.onCreateView(inflater, container, savedInstanceState);
			 
			 View v = inflater.inflate(R.layout.e_frm_main, container, false);
			 btnStart = (Button)v.findViewById(R.id.btnStart);
			 btnStart.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {	
					if (!CheckFullListModel()) {
						// xuat thong bao ko co database o day
						ShowMessageBox(v.getContext());						
					}
					else
						gotoNextPage(holder.start);
				}
			 });
			 
			 btnOptions = (Button)v.findViewById(R.id.btnOptions);
			 btnOptions.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Log.d("click options", "Ham click");
					gotoNextPage(holder.options);
				}
			 });
			 
			 btnIntro = (Button)v.findViewById(R.id.btnIntro);
			 btnIntro.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					gotoNextPage(holder.intro);
				}
			 });
			 
			 btnExit = (Button)v.findViewById(R.id.btnExit);
			 btnExit.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					android.os.Process.killProcess(android.os.Process.myPid());
					System.exit(1);
				}
			});
		 
			 return v;
	 }
	 
	 private boolean CheckFullListModel(){
		 int sizeShowModel = 10;
		 EDatabaseHelper db = new EDatabaseHelper(getActivity().getApplicationContext());
		 Setting settings = Setting.newInstance();
		 List<Model> listModel = db.getAllModels(sizeShowModel, settings.getSubject(), settings.getType(), settings.getLevel());
		 
		 if (listModel.size() < sizeShowModel)
			 return false;
		 return true;
	 }
	 
	 private void ShowMessageBox(Context context) {
		 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
		 
		 alertDialogBuilder.setTitle("Warning");
		 alertDialogBuilder.setMessage("Can't get database from server. Please check your internet connection!")
		 .setCancelable(false)
		 .setPositiveButton("OK",new DialogInterface.OnClickListener() {
			 public void onClick(DialogInterface dialog,int id) {
				 // if this button is clicked, close
				 android.os.Process.killProcess(android.os.Process.myPid());
				 System.exit(1);
			 }
		  });
	 
		 AlertDialog alertDialog = alertDialogBuilder.create();
		 alertDialog.show();
	}
	
	private final static class ViewHolder{
		Fragment start;
		Fragment options;
		Fragment intro;
		
		public static ViewHolder newInstance(Bundle bundle){
			ViewHolder holder = new ViewHolder();
			holder.initFragments(bundle);
			
			return holder;
		}
		
		public void initFragments(Bundle bundle){
			start = EMainFragmentPronounciation.newInstance(bundle);
			options = EMainFragmentOptions.newInstance(bundle);
			intro = new EMainFragmentIntroduction();
		}
	}

	@Override
	public void gotoNextPage(Fragment frm) {
		eFragmentTransaction = eFragmentManager.beginTransaction();
		eFragmentTransaction.replace(R.id.content_frame, frm);
		eFragmentTransaction.addToBackStack(null);
	 	eFragmentTransaction.commit();
	}

}
